<template>
	<view :style="{ height: conentHeight + 'px;' }">
		<view class="top-height">
			<view class="page-top-box">
				<view class="search-box flex ac">
					<u-search
						borderColor="#DADADA"
						bgColor="#ffffff"
						placeholder="搜索项目名称"
						v-model="searchForm.project_name"
						:showAction="false"
						@search="searchFunc"
						@clear="searchFunc"
					></u-search>
					<u-button
						@click="navToAddFunc"
						v-if="$isPer(per_arr, ['contract_add'])"
						customStyle="width: 156rpx;height: 64rpx;background: #3B75FF;border-radius: 32rpx;border-color:#F8F8FA;font-weight: 400;font-size: 26rpx;color: #ffffff;margin-left:20rpx;"
						:hairline="false"
						text="新增合同"
					></u-button>
				</view>
			</view>
			<view class="search-tab-box flex ac">
				<view class="tab-width">
					<u-tabs
						:list="searchTabList"
						lineHeight="0"
						inactiveStyle="width: 150rpx;height: 52rpx;background: #F8F8FA;border-radius: 26rpx;font-weight: 400;font-size: 26rpx;color: #919092;line-height:52rpx;text-align:center;"
						activeStyle="width: 150rpx;height: 52rpx;background: #3B75FF;border-radius: 26rpx;font-weight: 500;font-size: 26rpx;color: #FFFFFF;line-height:52rpx;text-align:center;"
						@click="searchTabClick"
						:current="searchForm.tabIndex"
					></u-tabs>
				</view>
				<view class="search-btn flex ac" @click="searchShow = true" :style="searchShow ? 'color:#3B75FF;' : ''">
					筛选
					<text class="iconfont icon-shaixuan" :style="searchShow ? 'color:#3B75FF;' : ''"></text>
				</view>
			</view>
		</view>
		<view class="list-content-box" :style="{ height: 'calc(100% - ' + topHeight + 'px);' }">
			<z-paging ref="paging" :fixed="false" v-model="dataList" @query="queryList" :paging-style="{ 'background-color': '#f7f7f7', height: '100%', width: '100%' }">
				<template slot="empty">
					<u-empty icon="https://hzybucket.obs.cn-south-1.myhuaweicloud.com/we_chat/no-msg2.png" text=" "></u-empty>
				</template>
				<view class="list-items" :key="index" v-for="(item, index) in dataList" @click.stop="navToDetailFunc(item, index)">
					<view class="flex ac flex-wrap">
						<view class="list-items-title flex ac jb" v-if="$fieldShowFunc('ht_xmmc')">
							<view class="title u-line-1">
								{{ item.project_name || '--' }}
							</view>
							<u-tag color="#3B75FF" borderColor="rgba(0,0,0,0)" bgColor="rgba(59, 117, 255, .15)" text="已签约" size="mini" v-if="item.contract_state == 1"></u-tag>
							<u-tag color="#909399" borderColor="rgba(0,0,0,0)" bgColor="rgba(144, 147, 153, .15)" text="已废弃" size="mini" v-if="item.contract_state == 2"></u-tag>
						</view>
						<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('ht_xmlx')">
							<view class="title">项目类型</view>
							<view class="desc u-line-1">
								<template v-if="item.contract_type == 1">施工合同</template>
								<template v-if="item.contract_type == 2">勘场合同</template>
								<template v-if="item.contract_type == 3">增减项合同</template>
								<template v-if="item.contract_type == 4">定金合同</template>
							</view>
						</view>
						<view class="list-items-item flex ac" v-if="$fieldShowFunc('ht_cjsj')">
							<view class="title">创建时间</view>
							<view class="desc u-line-1">
								{{ item.created_at || '--' }}
							</view>
						</view>
						<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('ht_heqyje')">
							<view class="title">合同签约金额</view>
							<view class="desc u-line-1">
								{{ item.contract_price + '' ? item.contract_price + '元' : '--' }}
							</view>
						</view>
						<view class="list-items-item flex list-items-item-width50">
							<view class="title">合同已收款</view>
							<view class="desc u-line-1">
								{{ item.paid_amount + '' ? item.paid_amount + '元' : '--' }}
							</view>
						</view>
						<view class="list-items-item flex ac list-items-item-width50">
							<view class="title">合同未收款</view>
							<view class="desc u-line-1">
								{{ item.unpaid_amount + '' ? item.unpaid_amount + '元' : '--' }}
							</view>
						</view>
						<!-- 按钮 -->
						<view style="width: 100%">
							<judgeBtn plain :per_arr="per_arr" :btns="btns" :state="item.contract_state" :item="item" :idx="index" @change="btnsChange"></judgeBtn>
						</view>
					</view>
				</view>
			</z-paging>
		</view>
		<!-- 筛选弹窗 -->
		<u-popup :show="searchShow" round="30" @close="searchShow = false" closeable>
			<view class="search-popup-title">全部筛选</view>
			<scroll-view scroll-y class="search-popup-box">
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>项目类型</text>
						<view
							class="search-items-title-right flex ac jc"
							hover-class="hover-class"
							v-if="searchListObj.store_type_id.page != 1"
							@click="retractFunc('store_type_id')"
						>
							<text class="txt">收起</text>
							<u-icon name="arrow-up" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('store_type_id', item)"
							:class="item.id == searchForm.store_type_id ? 'search-items-list-item-active' : ''"
							class="search-items-list-item"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.store_type_id.list"
							:key="index"
						>
							{{ item.store_type_name }}
						</view>
						<view
							class="search-items-list-more flex ac jc"
							hover-class="hover-class"
							@click="lookMoreFunc('store_type_id')"
							v-if="searchListObj.store_type_id.list.length != searchListObj.store_type_id.max"
						>
							<text class="txt">查看更多</text>
							<u-icon name="arrow-down" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title">合同签约金额</view>
					<u--input type="number" placeholder="合同签约金额" border="surround" v-model="searchForm.contract_price"></u--input>
				</view>
				<view class="search-items">
					<view class="search-items-title">合同已收款</view>
					<u--input type="number" placeholder="合同已收款" border="surround"></u--input>
				</view>
				<view class="search-items">
					<view class="search-items-title">合同未收款</view>
					<u--input type="number" placeholder="合同未收款" border="surround"></u--input>
				</view>
				<view class="search-items">
					<view class="search-items-title">创建时间</view>
					<view @click="chooseDateFunc">
						<u--input readonly placeholder="创建时间" border="surround" :value="searchForm.contract_commencement_show"></u--input>
					</view>
				</view>
			</scroll-view>
			<view class="btn-box flex ac jb">
				<u-button
					@click="reSearchValFunc"
					text="重置"
					customStyle="width: 248rpx;height: 82rpx;background: #242834;border-radius: 12rpx;font-weight: 500;font-size: 30rpx;color: #FFFFFF;"
				></u-button>
				<u-button
					@click="searchFunc"
					text="查询"
					customStyle="width: 422rpx;height: 82rpx;background: #3B75FF;border-radius: 12rpx;font-weight: 500;font-size: 30rpx;color: #FFFFFF;"
				></u-button>
			</view>
		</u-popup>
		<!-- 创建时间筛选 -->
		<u-calendar
			:show="dateShow"
			mode="range"
			@confirm="dateConfirmFunc"
			@close="dateShow = false"
			:maxDate="maxDate"
			:minDate="minDate"
			:defaultDate="defaultDate"
			:monthNum="36"
		></u-calendar>

		<contractManagement :value.sync="per_name" :oper_item="oper_item" @submit="handleSubmit"></contractManagement>
	</view>
</template>

<script>
export default {
	name: 'contractManagement',
	props: {
		conentHeight: {
			default: 0
		},
		option: {
			type: Object,
			default: {}
		}
	},
	data() {
		const d = new Date();
		const year = d.getFullYear();
		let month = d.getMonth() + 1;
		month = month < 10 ? `0${month}` : month;
		const date = d.getDate();
		return {
			per_arr: [],
			btns: [
				{
					per_name: 'contract_contractInvalidation',
					state: 1,
					name: '合同作废'
				}
			],
			oper_item: {},
			per_name: '',
			oper_idx: '',

			defaultDate: [`${year}-${month}-${date}`],
			maxDate: `${year}-${month}-${date}`, //日历最大日期
			minDate: `${year - 2}-${month}-${date}`, //日历最小日期
			dateShow: false,
			searchShow: false,
			dataList: [],
			searchForm: {
				keyword: '',
				tabIndex: 0,
				store_type_id: '',
				contract_price: '',
				contract_commencement_date_start: '',
				contract_commencement_date_end: '',
				contract_commencement_show: ''
			},
			topHeight: 0,
			searchTabList: [
				{
					name: '施工合同',
					value: 1
				},
				{
					name: '勘场合同',
					value: 2
				},
				{
					name: '增减项合同',
					value: 3
				},
				{
					name: '定金合同',
					value: 4
				}
			],
			searchListObj: {
				store_type_id: {
					list: [],
					page: 1,
					num: 5,
					max: 0
				}
			}
		};
	},
	mounted() {
		this.getStoreTypeList();
		this.getHeight();
		// 获取权限
		this.$Api
			.getHtmlAuthority({
				path: '/contractManagement'
			})
			.then((res) => {
				console.log(res, 'getHtmlAuthority');
				if (res.code == 200) {
					this.per_arr = res.data.permission;
				}
			});
	},
	watch: {},
	methods: {
		// 重置弹窗筛选项
		reSearchValFunc() {
			this.searchForm = {
				project_name: '',
				tabIndex: 0,
				store_type_id: '',
				contract_price: '',
				contract_commencement_show: '',
				contract_commencement_date_start: '',
				contract_commencement_date_end: ''
			};
			this.$nextTick(() => {
				this.searchFunc();
			});
		},
		// 刷新列表
		searchFunc() {
			this.$refs.paging.reload();
			this.searchShow = false;
		},
		getHeight() {
			// 创建节点查询
			let that = this;
			const systemInfo = uni.getSystemInfoSync();
			const query = uni.createSelectorQuery().in(that);
			query.select('.top-height').boundingClientRect();
			query.exec((res) => {
				that.topHeight = res[0].height + systemInfo.statusBarHeight + 20;
			});
		},
		// 去新增
		navToAddFunc() {
			uni.navigateTo({
				url: '/pagesC/contractManagement/contractManagement'
			});
		},
		// 去详情
		navToDetailFunc(item, index) {
			this.oper_idx = index;
			uni.navigateTo({
				url: '/pagesB/contractDetail/contractDetail?id=' + item.id
			});
		},
		queryList(page, num) {
			this.getContractList(page, num);
		},
		// 合同列表
		getContractList(page, num) {
			uni.showLoading({
				mask: true
			});
			let searchForm = this.searchForm;
			this.$Apipc
				.getContractList({
					page,
					num,
					contract_type: Number(this.searchTabList[searchForm.tabIndex].value),
					store_type_id: searchForm.store_type_id,
					project_name: searchForm.project_name,
					contract_price: searchForm.contract_price,
					contract_commencement_date_start: searchForm.contract_commencement_date_start,
					contract_commencement_date_end: searchForm.contract_commencement_date_end
				})
				.then((res) => {
					uni.hideLoading();
					console.log(res, '合同列表');
					if (res.code == 200) {
						this.$refs.paging.complete(res.data.list);
					}
				});
		},
		// tab筛选
		searchTabClick(e) {
			this.searchForm.tabIndex = e.index;
			this.$refs.paging.reload();
		},
		// 门店类型列表
		getStoreTypeList() {
			this.$Apipc
				.getStoreTypeList({
					page: this.searchListObj.store_type_id.page,
					num: this.searchListObj.store_type_id.num
				})
				.then((res) => {
					console.log(res, '门店类型列表');
					if (res.code == 200) {
						if (this.searchListObj.store_type_id.page == 1) {
							this.searchListObj.store_type_id.list = [];
						}
						this.searchListObj.store_type_id.list = [...this.searchListObj.store_type_id.list, ...res.data.list];
						this.searchListObj.store_type_id.max = res.data.max;
					}
				});
		},

		// 弹窗筛选项查看更多
		lookMoreFunc(type) {
			switch (type) {
				case 'store_type_id':
					this.searchListObj.store_type_id.page += 1;
					this.getStoreTypeList();
					break;
				default:
			}
		},
		// 弹窗筛选项列表收起
		retractFunc(type) {
			switch (type) {
				case 'store_type_id':
					this.searchListObj.store_type_id.page = 1;
					this.getStoreTypeList();
					break;
				default:
			}
		},
		// 弹窗筛选项
		searchSelectFunc(type, item) {
			console.log(type, item);
			switch (type) {
				case 'store_type_id':
					this.searchForm[type] = this.searchForm[type] == item.id ? '' : item.id;
					break;
				default:
			}
		},
		// 创建时间日历弹窗
		chooseDateFunc() {
			this.dateShow = true;
		},
		// 日期筛选确认
		dateConfirmFunc(e) {
			this.searchForm.contract_commencement_date_start = e[0];
			this.searchForm.contract_commencement_date_end = e[e.length - 1];
			this.searchForm.contract_commencement_show = e[0] + '~' + e[e.length - 1];
			this.dateShow = false;
		},

		btnsChange(e) {
			console.log(e, 'xxxsww');
			this.oper_item = e.item;
			this.oper_idx = e.index;
			this.per_name = e.per_name;
		},
		handleSubmit() {
			if (this.per_name == 'contract_contractInvalidation') {
				this.dataList[this.oper_idx].contract_state = 2;
			}
		},

		refreshItemFunc() {
			let idx = this.oper_idx.toString();
			if (idx) {
				let item = this.dataList[idx];
				this.$Apipc
					.getContractDetails({
						contract_id: item.id
					})
					.then((res) => {
						console.log(res, '合同详情');
						if (res.code == 200) {
							let info = res.data;
							this.dataList[idx].contract_state = res.data.contract_state;
							this.dataList.splice(0, 0);
						}
					});
			}
		}
	}
};
</script>

<style lang="scss"></style>
